package com.ryqg.jiaofu.config.security.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class WechatAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;

    /**
     * 未认证
     * @param principal 主体
     */
    public WechatAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        super.setAuthenticated(false);
    }

    /**
     * 已认证
     * @param principal 主体
     * @param authorities 权限
     */
    public WechatAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    /**
     * 认证通过
     * @param principal 主体
     * @param authorities 权限
     * @return WechatAuthenticationToken
     */
    public static WechatAuthenticationToken authenticated(Object principal,
                                                                    Collection<? extends GrantedAuthority> authorities) {
        return new WechatAuthenticationToken(principal, authorities);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }
}
